package userInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.rmi.CORBA.Util;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.colorchooser.ColorChooserComponentFactory;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.ColorChooserUI;
import javax.swing.text.Utilities;

public class Drawing 
{	
	private static JFrame _frame;
	private static RootView _view;
	private static RootController _controller;
	private static DrawingModel _model;
	private static JFrame _frame2;
	
	private static FileFilter mvcFileFilter = new FileFilter()
	{
		
		@Override
		public String getDescription()
		{
			return "User Interface Class (.mvc)";
		}
		
		@Override
		public boolean accept(File f)
		{
			if (f.isDirectory())
				return true;
			String path = f.getAbsolutePath();
			path = path.substring(path.lastIndexOf('.'));
			if (path.equals(".mvc"))
				return true;
			return false;
		}
	}; 
	
	private static final ActionListener ColorAction = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			Color c = JColorChooser.showDialog(_view, "blah", new Color(193, 213, 221));
			_controller.setSelectedColor(c);
			
		}
	};
	
	private static final ActionListener ThicknessAction = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			JDialog dialog = new JDialog(_frame, "blah");
			dialog.setVisible(true);
//			dialog.add
		}
	};

	private static final ActionListener OpenAction = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			JFileChooser fc = new JFileChooser();
			fc.setFileFilter(mvcFileFilter);
			if (fc.showOpenDialog(_view) == JFileChooser.APPROVE_OPTION)
				_model.open(fc.getSelectedFile().getAbsolutePath());
		}
	};

	private static final ActionListener SaveAction = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			JFileChooser fc = new JFileChooser();
			fc.showSaveDialog(_view);
		}
	};

	private static final ActionListener SaveAsAction = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			JFileChooser fc = new JFileChooser();
			if (fc.showSaveDialog(_view) == JFileChooser.APPROVE_OPTION)
			{
				_model.setFileName(fc.getSelectedFile().getAbsolutePath());
				_model.save();
			}
		}
	};

	private static JMenuBar createMenu()
	{
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBackground(new Color(193, 213, 221));
		menuBar.setBorderPainted(false);
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		fileMenu.setBorderPainted(false);

		JMenuItem open = new JMenuItem("Open");
		JMenuItem save = new JMenuItem("Save");
		JMenuItem saveAs = new JMenuItem("Save As...");
		open.addActionListener(OpenAction);
		save.addActionListener(SaveAction);
		saveAs.addActionListener(SaveAsAction);
		
		fileMenu.add(open);
		fileMenu.addSeparator();
		fileMenu.add(save);
		fileMenu.add(saveAs);
		
		JMenu setMenu = new JMenu("Set");
		setMenu.setMnemonic(KeyEvent.VK_S);
		setMenu.setBorderPainted(false);
		
		JMenuItem color = new JMenuItem("Color");
		JMenuItem thickness = new JMenuItem("Thickness");
		color.addActionListener(ColorAction);
		thickness.addActionListener(ThicknessAction);

		setMenu.add(color);
		setMenu.add(thickness);
		
		menuBar.add(fileMenu);
		menuBar.add(setMenu);
		
		return menuBar;
	}
	
	public static void main(String[] args) 
	{
		// some testing methods
		//DrawingModel.Test();		
		
		// setup model view and controller
		if (args.length == 0)
			_model = new DrawingModel("src/scene1.txt");
		else
			_model = new DrawingModel(args[0]);
		_view = new RootView(_model);
		_controller = new RootController(_view, _model);
		
		// setup the frame
		_frame = new JFrame("User Interface 1.0");
		_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// add the menu
		_frame.setJMenuBar(createMenu());
		
		// add the view
		_frame.getContentPane().add(_view);
		
		// set bounds and visibility
		_frame.setBounds(10, 10, 1000, 600);
		_frame.setVisible(true); 		
	}

}
